import 'package:search_people/entrance/route/routes.dart';
import 'package:get/get.dart';
import 'model/welcome.dart';

import 'index.dart';

class WelcomeController extends GetxController {
  WelcomeController();

  final state = WelcomeState();

  void _observer() {
    state.pageCtrl.addListener(() {
      var _maxOffset = ((state.list?.length ?? 1) - 1) * Get.width + 50;
      if (state.pageCtrl.offset > _maxOffset) {
        toMain();
      }
    });
  }

  void toMain() {
    Get.offAllNamed(RouterPath.bottom_nav);
  }

  void _getData() {
    state.list = [
      WelcomeModel()
        ..url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcos2.solepic.com%2FUploadImg%2F20200302%2F202003021550553133.jpg&refer=http%3A%2F%2Fcos2.solepic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645146260&t=60d1d981a4779b41191d9e168312721c",
      WelcomeModel()
        ..url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcos2.solepic.com%2F20200513%2Fb_955483202005131401152796.png&refer=http%3A%2F%2Fcos2.solepic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645146260&t=043e6a5dfa890ead4e5f756555df4878",
      WelcomeModel()
        ..url =
            "http://www.news.cn/photo/2022-01/18/1128275280_16425010487911n.jpg",
    ];
    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    _observer();
    _getData();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.pageCtrl.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
